--[[
LuCI - Lua Configuration Interface

Copyright 2014 Steven Barth <emongxx@gmail.com>
Blog http://blog.emong.me

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

$Id$
]]--


m = Map("kxsw", translate("科学上网"),
	translate("通过shadowsocks和redsocks2来实现的科学上网。"))

function m.on_commit(self)
        luci.sys.exec("/etc/init.d/kxsw start")                                                   
end 


s = m:section(TypedSection, "redsocks2", translate("Redsocks"),translate("Redsocks自动判断代理工具，先直连，直连不上时通过下方的代理服务器连接。"))

enable = s:option(Flag, "enable", translate("启用"))
enable.default = false
enable.rmempty = false

local_port = s:option(Value, "local_port", translate("监听端口"), translate("请保证此端口的唯一性！"))
local_port.default = 7060
local_port.rmempty = true

dports = s:option(Value, "dports", translate("走代理的tcp端口"),translate("支持多个端口，如80,443,2000:3000，留空则代理tcp协议的所有端口。"))
dports.rmempty = true

proxy_ip = s:option(Value, "proxy_ip",  translate("上层代理服务器IP地址"), translate("可以和shadowsocks配合使用。"))
proxy_ip.default = "1.2.3.4"

proxy_port = s:option(Value, "proxy_port", translate("上层代理服务器端口"))
proxy_port.default = "1080"

proxy_type = s:option(ListValue, "proxy_type", translate("上层代理类型"))
proxy_type:value("http-relay")
proxy_type:value("http-connect")
proxy_type:value("socks5")
proxy_type.default = "socks5"

autoproxy = s:option(Flag, "autoproxy", translate("自动代理"))
autoproxy.default = 1
autoproxy.rmempty = false

timeout = s:option(Value, "timeout", translate("超时"), translate("单位:S。"))
timeout.default = 3

ipsetreverse = s:option(Flag, "ipsetreverse", translate("代理非列表内的地址"),translate("勾选后会对列表之外的地址进行代理。程序会自动排除私网地址和组播地址。"))
ipsetreverse.default = 1
ipsetreverse.rmempty = false

ipset = s:option(Value,"_ipset","代理ip列表")
ipset.template = "cbi/tvalue"
ipset.rows = 20
function ipset.cfgvalue(self, section)
	return nixio.fs.readfile("/etc/kxsw/"..tostring(section))
end
function ipset.write(self, section, value)
	value = value:gsub("\r\n?", "\n")
	nixio.fs.writefile("/tmp/etc/kxsw/"..tostring(section), value)
	luci.sys.exec("md5sum /tmp/etc/kxsw/"..tostring(section).." /etc/kxsw/"..tostring(section).." |awk '{print $1}' |tr '\n' ' ' |awk '{if($1!=$2)exit 0;else exit 1}' && cp /tmp/etc/kxsw/"..tostring(section).." /etc/kxsw/"..tostring(section)..";rm /tmp/etc/kxsw/"..tostring(section))
end

return m
